package com.kinglone.back.userPKRole.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.kinglone.back.user.model.User;
import com.kinglone.back.userPKRole.service.UserPKRoleService;
import com.kinglone.common.utils.JSONMessageView;

@Controller
@RequestMapping("/back/userPKRole")
public class UserPKRoleController {

	@Autowired
	private UserPKRoleService userPKRoleService;
	
	/**
	 * 解绑用户
	 * @param userIds
	 * @param roleId
	 * @return
	 */
	@RequestMapping("/unBindUser.htm")
	public String unBindUser(@RequestParam(value="userIds")String userIds,@RequestParam(value="roleId")String roleId){
		
		 try {
			 userPKRoleService.unBindUser(userIds,roleId);			  
		  } catch (Exception e) {
			  e.printStackTrace();
		  }
		  return "forward:/back/role/role-add-user.htm";
		
	}
	/**
	 * 跳转未绑定的用户列表
	 * @param roleId
	 * @return
	 */
	@RequestMapping("/showBindUser.htm")
	public ModelAndView showBindUser(@RequestParam(value="roleId")String roleId){
		ModelAndView mv = new ModelAndView("back/bindUser");
		List<User> userList = userPKRoleService.findUnBindUsers(roleId);
		mv.addObject("userList", userList);
		mv.addObject("roleId", roleId);
		return mv;
	}
	/**
	 * 绑定用户
	 * @param userIds
	 * @param roleId
	 * @return
	 */
	@RequestMapping("/bindUser.htm")
	public JSONMessageView bindUser(@RequestParam(value="userIds")String userIds,@RequestParam(value="roleId")String roleId){
		JSONMessageView msg = new JSONMessageView();
		try {
			userPKRoleService.bindUser(userIds,roleId);
			msg = new JSONMessageView(0,"绑定成功!",null);
			return msg;
		} catch (Exception e) {
			e.printStackTrace();
			msg = new JSONMessageView(0,"绑定失败!",null);
			return msg;
		}

	}
}
